import {View, Text, Image, ScrollView} from '@tarojs/components'
import {useState} from 'react'
import http from '../../../http/http'


const TipList = ({allGoods, allGoodsData}) => {


  // 增加数量
  const add = (v) => {
    // 调接口改变数量
    http({
      url: '/huangyi/addGoodsNum',
      method: 'post',
      data: {
        id: v.id,
        num: v.num + 1
      }
    }).then(res => {
      console.log(res)
      allGoodsData()
    })
  }

  // 减少数量
  const reduce = (v) => {
    // 调接口改变数量
    http({
      url: '/huangyi/reduceGoodsNum',
      method: 'post',
      data: {
        id: v.id,
        num: v.num - 1
      }
    }).then(res => {
      console.log(res)
      allGoodsData()
    })
  }


  return (
    <View className='tipList'>
      <ScrollView
        className='tipList_scrollview'
        scrollY
        enableBackToTop
      >
        {allGoods.map(v => (<View style={{display: 'flex', margin: '24px 0'}} key={v.id}>
          <Image src={v.image_url}></Image>
          <View className='tipList_con'>
            <View style={{textAlign: 'start'}}>{v.information}</View>
            <View className='tipList_price'>
              <View>
                <View className='newPrice'>￥{v.cost_price}</View>
                <View className='oldPrice'>￥{v.sell_price}</View>
              </View>
              <View style={{display: 'flex', alignItems: 'center'}}>
                <View className={[v.num === 0 ? 'hidden' : '', 'operat']} onClick={() => reduce(v)}>-</View>
                <View className={[v.num === 0 ? 'hidden' : '', 'num']} >{v.num}</View>
                <View className='operat' onClick={() => add(v)}>+</View>
              </View>
            </View>

          </View>
        </View>
        ))}
      </ScrollView>


    </View>
  )
}

export default TipList